package sample;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author caiwl
 * @date 2020/6/11 16:35
 */
public class ConnectionUtil {
    public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1:3306";
        String user = "root";
        String password = "mysql";

        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);
            st = conn.createStatement();
            st.execute("select SCHEMA_NAME from information_schema.SCHEMATA order by SCHEMA_NAME");
            rs = st.getResultSet();
            List<String> dbs = new ArrayList<>();
            while (rs.next()) {
                dbs.add(rs.getString(1));
            }
            System.out.println(dbs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
            } catch (SQLException e) {
            } finally {
                try {
                    st.close();
                } catch (SQLException e) {
                } finally {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                    }
                }
            }
        }
    }
}
